﻿using System;
using System.Threading;

namespace LExperiment.Master
{
    static class TestWholeLaunch
    {
        public static void TestMain()
        {
            using (LMaster master = new LMaster("localhost", 6001))
            {
                int[][] subscriberIds = new int[2][] 
                {
                    new int[] {1,2,3},
                    new int[] {4,5}
                };
                int[] ports = new int[] { 7001, 7002 };

                for (int index = 0; index < 2; ++index)
                {
                    master.Start("localhost", ports[index], subscriberIds[index]);
                    Console.WriteLine("[+] press any button to start next, ......");
                    Console.ReadLine();
                }

                Console.WriteLine("press any button to stop, ......");
                Console.ReadLine();

                for (int index = 0; index < 2; ++index)
                {
                    master.Stop(subscriberIds[index]);
                    Console.WriteLine("[-] press any button to stop next, ......");
                    Console.ReadLine();
                }
            }
        }
    }

    static class TestOneByOneLaunch
    {
        public static void TestMain()
        {
            using (LMaster master = new LMaster("localhost", 6001))
            {
                int numSubProcs = 10;
                int[] subscriberIds = new int[numSubProcs];
                for (int index = 0; index < numSubProcs; ++index)
                    subscriberIds[index] = index;

                int port = 0;
                foreach (int id in subscriberIds)
                {
                    port = ((id % 2 == 0) ? 7001 : 7002);
                    master.Start("localhost",port,new int[]{id});

                    Thread.Sleep(500);
                }

                Console.WriteLine("Press any key to start stopping, ......");
                Console.ReadLine();

                foreach (int id in subscriberIds)
                {
                    master.Stop(new int[]{id});
                    Thread.Sleep(500);
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Test.TestSingleLaunch.TestMain();
            // TestWholeLaunch.TestMain();
            TestOneByOneLaunch.TestMain();
        }
    }
}
